package com.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import androidx.fragment.app.Fragment;

import com.Dao.NewsDao;
import com.adapter.NewsAdapter;
import com.example.myapplication.ArticleActivity;
import com.example.myapplication.R;
import com.google.android.material.tabs.TabLayout;
import com.news.model.News;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private ListView listView;
    private NewsDao newsDao;
    public HomeFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment HomeFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static HomeFragment newInstance(String param1, String param2) {
        HomeFragment fragment = new HomeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        // 设置TabLayout 初始化listview
        newsDao = new NewsDao(requireContext()); // 或者使用依赖注入等方式
        listView = (ListView) view.findViewById(R.id.news_view);
        TabLayout tabLayout = view.findViewById(R.id.tabs);
        tabLayout.addTab(tabLayout.newTab().setText("推荐"));
        tabLayout.addTab(tabLayout.newTab().setText("热点"));
        tabLayout.addTab(tabLayout.newTab().setText("军事"));
        tabLayout.addTab(tabLayout.newTab().setText("经济"));
        tabLayout.addTab(tabLayout.newTab().setText("政治"));
        tabLayout.addTab(tabLayout.newTab().setText("外交"));
        tabLayout.addTab(tabLayout.newTab().setText("其它"));

        // 设置Tab点击事件
        // 设置Tab点击事件
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                switch (position) {
                    case 0:
                        filterNews("推荐");
                        break;
                    case 1:
                        filterNews("热点");
                        break;
                    case 2:
                        filterNews("军事");
                        break;
                    case 3:
                        filterNews("经济");
                        break;
                    case 4:
                        filterNews("政治");
                        break;
                    case 5:
                        filterNews("外交");
                        break;
                    case 6:
                        filterNews("其它");
                        break;
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                // 可以在这里处理标签未被选中时的逻辑
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                // 可以在这里处理标签被重新选择时的逻辑
            }
        });

/*
        //访问新闻表
        List<News> newslist = newsDao.getAllNews();
        // 创建NewsAdapter
        NewsAdapter newsAdapter = new NewsAdapter(requireContext(), newslist);
        // 设置ListView的适配器
        listView.setAdapter(newsAdapter);*/
        // 默认加载“推荐”类别的新闻
        filterNews("推荐");
        // 设置ListView的点击事件监听器
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // 获取被点击的新闻对象
                News selectedNews = (News) parent.getItemAtPosition(position);
                // 创建Intent，跳转到新闻详情Activity
                Intent intent = new Intent(requireContext(), ArticleActivity.class);
                // 传递新闻ID
                intent.putExtra("newsId", selectedNews.getNewsId());
                // 启动新闻详情Activity
                startActivity(intent);
            }
        });
    }
    private void filterNews(String category) {
        // 根据类别过滤新闻
        Log.e("过滤","过滤新闻方法调用");
        List<News> filteredNews = newsDao.getNewsByCategory(category);
        for(News news:filteredNews){
            Log.e("数据",news.toString());
        }
        if (filteredNews != null && !filteredNews.isEmpty()) {
            Log.e("数据","有数据");
            NewsAdapter newsAdapter = new NewsAdapter(requireContext(), filteredNews);
            listView.setAdapter(newsAdapter);
            newsAdapter.notifyDataSetChanged(); // 通知数据改变
        } else {
            // 处理没有数据的情况，显示一个空白列表
            Log.e("数据","没有数据");
            NewsAdapter newsAdapter = new NewsAdapter(requireContext(), new ArrayList<News>());
            listView.setAdapter(newsAdapter);
            newsAdapter.notifyDataSetChanged(); // 通知数据改变
        }
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        // 关闭数据库连接
        newsDao.close();
    }

}